package ua.eug.airv1.weapon;

import ua.eug.airv1.ImageProvider;
import ua.eug.airv1.controllers.ScreenController;
import ua.eug.airv1.ground.Airfield;
import ua.eug.airv1.ground.AntiAirCannon;
import ua.eug.airv1.map.IOnScreen;
import ua.eug.airv1.map.MapHolder;
import ua.eug.airv1.map.Tile;
import ua.eug.airv1.map.TilePos;
import ua.eug.airv1.orders.OrderHolder;
import ua.eug.airv1.orders.WeaponOrder;
import ua.eug.airv1.plane.Bomber;
import ua.eug.airv1.plane.Fighter;
import ua.eug.airv1.utils.EDirection;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.utils.Array;

public class WeaponHolder extends Group implements IOnScreen {

	public static final TextureRegion bomber[] = new TextureRegion[2];
	public static final TextureRegion fighter[] = new TextureRegion[2];
	public static final TextureRegion antiAirCannon[] = new TextureRegion[2];
	
	private final Array<IWeapon> weapon = new Array<IWeapon>(false,64);
	private ExplosionPool explosions = ExplosionPool.getInstance();
	
	// Move weapon as ordered
	private Boolean gogogo = false;
	private Boolean afterGo = false;
	private int nextOrderPos = 0;
	
	protected WeaponHolder() {		

		bomber[0] = ImageProvider.FindRegion("bomber",1);
		fighter[0] = ImageProvider.FindRegion("fighter",1);
		bomber[1] = ImageProvider.FindRegion("bomber",2);
		fighter[1] = ImageProvider.FindRegion("fighter",2);
		antiAirCannon[0] = ImageProvider.FindRegion("missile",1);
		antiAirCannon[1] = ImageProvider.FindRegion("missile",2);

		ScreenController.getInstance().addSlave(this);
	}
	
	private static class SingletonHolder {
        public static final WeaponHolder HOLDER = new WeaponHolder();
    }
	
	public static WeaponHolder getInstance() {
        return SingletonHolder.HOLDER;
    }

	@Override
	public void pan(float x, float y) {
		this.setPosition(x, y);
	}

	@Override
	public void zoom(float z) {
		this.setScale(z);
	}

	public void test() {
		Tile t = MapHolder.getInstance().getTile(4, 5);
		AntiAirCannon b2 = new AntiAirCannon(0,t);
		b2.alive = true;
		weapon.add(b2);
		this.addActor(b2); 
		
		t = MapHolder.getInstance().getTile(6, 7);
		Airfield a2 = new Airfield(0,t);
		weapon.add(a2);
		this.addActor(a2); 		

		t = MapHolder.getInstance().getTile(3, 3);
		Fighter f = new Fighter(0,t);
		f.alive = true;
		f.turn(EDirection.NEE);
		weapon.add(f);
		this.addActor(f); 
		
		t = MapHolder.getInstance().getTile(3, 9);
		Bomber b = new Bomber(0,t);
		b.alive = true;
		b.turn(EDirection.SEE);
		weapon.add(b);
		this.addActor(b); 

		t = MapHolder.getInstance().getTile(2, 6);
		Bomber b1 = new Bomber(1,t);
		b1.alive = true;
		b1.turn(EDirection.SEE);
		weapon.add(b1);
		this.addActor(b1); 

	}
	
	public Boolean getNextUnordered () {
		int i = nextOrderPos+1;
		if (i>=weapon.size)
			i = 0;
		while (true) {
			IWeapon w = weapon.get(i);

			if( w.getOwner() == 0 &&
					!w.getOrder().getOrderDone() ) {
				ScreenController.getInstance().pan(w.getTile().pos);
				break;
			}
			
			if(i == nextOrderPos) {
				// no unordered left
				return false;
			}
			if (++i>=weapon.size)
				i = 0;
		}
		nextOrderPos = i;
		return true;
	}
	
	public void executeOrders() {
		gogogo = true;
		OrderHolder.getInstance().setActive(WeaponOrder.ZOrder); // hide active order
	}
	
	@Override
	public void act(float f) {
		if (gogogo) {
			Boolean moved = false;
			
			for(IWeapon mover : weapon) {
				moved |= mover.aniMove(f);
			}
			
			if (!moved) { // end of all moves
								
				// kill enemy
				for(IWeapon mover : weapon) {
					mover.fireToEnemy();
				}

				gogogo = false;
				
				if (!explosions.isOnScreen()) {
					Gdx.graphics.setContinuousRendering(false);
					System.out.println("End move, "+ f);
				} else {
					afterGo = true;
				}
			}
			
		}
		
		if (afterGo) {
			if (!explosions.isOnScreen()) {
				Gdx.graphics.setContinuousRendering(false);
				System.out.println("End explosions, "+ f);
				afterGo = false;
			}
		}
	}
}
